<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-03 17:34:59
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-03 20:13:52
 */

namespace App\Models;

use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Str;

/**
 * App\Models\Tag
 *
 * @property int                                $id             标签主键
 * @property string                             $name           标签名
 * @property string                             $slug           slug
 * @property string                             $keywords       标签的关键字
 * @property string                             $description    标签的描述主要是 SEO 
 * @property \Illuminate\Support\Carbon|null    $created_at
 * @property \Illuminate\Support\Carbon|null    $updated_at
 * @property string|null                        $deleted_at
 */
class Tag extends Base
{
    /**
     * 关联文章表
     * 
     * @return BelongsToMany
     */
    public function articles()
    {
        return $this->belongsToMany(Article::class, 'article_tags');
    }

    /**
     * @return UrlGenerator|string
     */
    public function getUrlAttribute()
    {
        $parameters = [$this->id];

        if(Str::isTrue(config('wblog.seo.use_slug'))){
            $parameters[] = $this->slug;
        }

        return url('tag', $parameters);
    }
}
